<template>
  <div class="media-logo" @mouseenter="isHover = true" @mouseleave="isHover = false" :style="{ background: modelValue?.bgColor || 'transparent' }">
    <div class="media-logo-wrapper flex-center" :class="[modelValue?.width ? `container-content-${modelValue?.width}` : 'container-content']">
      <div v-if="modelValue?.data && modelValue?.data.length === 1" class="media-logo-img flex-center">
        <img
          v-for="item in modelValue?.data"
          :key="item.id"
          :src="item.imgSrc"
          :alt="item.alt"
        />
      </div>
      <div v-if="modelValue?.data && modelValue?.data.length > 1" class="media-logo-carousel">
        <client-only>
          <swiper
            ref="swiperRef"
            :modules="modules"
            :centered-slides="true"
            :slides-per-view="1"
            :speed="600"
            :pagination="{ el: '.swiper-pagination', clickable: true }"
            class="swiper media-logo-swiper"
          >
            <swiper-slide
              v-for="item in modelValue?.data"
              :key="item.id"
              class="swiper-slide"
            >
              <div class="media-logo-slide-content">
                <img :src="item.imgSrc" :alt="item.alt" />
              </div>
            </swiper-slide>
            <div class="swiper-pagination"></div>
          </swiper>
        </client-only>
      </div>
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Pagination } from 'swiper/modules'
import 'swiper/css'
import 'swiper/css/pagination'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'

// 定义组件名称
defineOptions({
  name: 'MediaLogo'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => ({})
  },
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'MediaLogo',
    bgColor: '',
    width: '',
    data: [
      {
        id: randomString(),
        imgSrc: '	https://ywies-tx.cedim.cn/_nuxt/img/logo-ycyw.0a99fd4.png',
        alt: 'logo-ycyw.png'
      },
      {
        id: randomString(),
        imgSrc: '	https://ywies-tx.cedim.cn/_nuxt/img/logo-ycyw.0a99fd4.png',
        alt: 'logo-ycyw.png'
      }
    ],
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()

const swiperRef = ref(null)
const modules = [Pagination]

</script>

<style lang="scss" scoped>
.media-logo-img,
.media-logo-carousel {
  height: 103px;

  @media (max-width: 767.98px) {
    height: 52.2px;
  }
}

.media-logo-img img {
  width: auto;
  height: 100%;
  object-fit: contain;
}

.media-logo-carousel {
  width: 100%;
  height: calc(103.02px + 16px);

  @media (max-width: 767.98px) {
    height: calc(52.2px + 16px);
  }
}

.media-logo-swiper {
  width: 100%;
  padding-bottom: 16px;
}

.media-logo-swiper ::v-deep .swiper-slide {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.media-logo-slide-content {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 103px;

  @media (max-width: 767.98px) {
    height: 52.2px;
  }
}

.media-logo-slide-content img {
  max-width: 100%;
  width: auto;
  height: 100%;
  object-fit: contain;
}

.media-logo-swiper .swiper-pagination {
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.media-logo-swiper .swiper-pagination ::v-deep .swiper-pagination-bullet {
  background-color: #0032a0;
  width: 8px;
  height: 8px;
  opacity: 0.4;

  @media (max-width: 767.98px) {
    width: 6px;
    height: 6px;
  }
}

.media-logo-swiper .swiper-pagination ::v-deep .swiper-pagination-bullet-active {
  opacity: 1;
  width: 10px;
  height: 10px;

  @media (max-width: 767.98px) {
    width: 8px;
    height: 8px;
  }
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>